package edu.cuit.zhuyimeng.aircraft.config;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheManager;
import edu.cuit.zhuyimeng.aircraft.controller.vo.AircraftInfoVO;
import edu.cuit.zhuyimeng.aircraft.dal.po.AirlinePO;
import edu.cuit.zhuyimeng.aircraft.enums.CacheConstants;
import edu.cuit.zhuyimeng.framework.cache.util.CacheUtils;
import edu.cuit.zhuyimeng.framework.common.system.enums.FlightStatus;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.List;

/**
 * 缓存配置类
 */
@Configuration
@RequiredArgsConstructor
public class CacheConfig {

    private final CacheManager cacheManager;

    private Cache<Long, AircraftInfoVO> aircraftInfoCache;
    private Cache<Long, AirlinePO> airlineInfoCache;
    private Cache<Integer, List<Long>> aircraftPageCache;
    private Cache<String, List<Long>> allAircraftCache;

    @PostConstruct
    public void init() {
        aircraftInfoCache = createCache(CacheConstants.AIRCRAFT_INFO);
        airlineInfoCache = createCache(CacheConstants.AIRLINE_INFO);
        aircraftPageCache = createCache(CacheConstants.AIRCRAFT_INFO_PAGER);
        allAircraftCache = createCache(CacheConstants.ALL_AIRCRAFT_INFO);
    }

    @Bean
    public Cache<Long, AircraftInfoVO> aircraftInfoCache() {
        return aircraftInfoCache;
    }

    @Bean
    public Cache<Long, AirlinePO> airlineInfoCache() {
        return airlineInfoCache;
    }

    @Bean
    public Cache<Integer, List<Long>> airlineInfoPageCache() {
        return aircraftPageCache;
    }

    @Bean
    public Cache<String, List<Long>> allAircraftCache() {
        return allAircraftCache;
    }

    private <A,B> Cache<A,B> createCache(String name) {
        return cacheManager.getOrCreateCache(CacheUtils.createCommonCache(name));
    }
}
